/*
 * Algorithm.h
 *
 *  Created on: 2023��12��1��
 *      Author: a
 */

#ifndef CODE_ALGORITHM_ALGORITHM_H_
#define CODE_ALGORITHM_ALGORITHM_H_






                /*******�ߵʹ�ͨ�˲������********/
                /*******�ߵʹ�ͨ�˲������********/
                /*******�ߵʹ�ͨ�˲������********/
/*��ͨ*/
typedef struct
{
  float alpha;                       //����ϵ��Proportional
  float Out;
  float Out_Last;
}Pass_Str;
                /*******�����˲������********/
                /*******�����˲������********/
                /*******�����˲������********/

#define MOVE_AVERAGE_SIZE   20  //���建������С

typedef struct
{
    unsigned char index;            //�±�
    unsigned char buffer_size;      //buffer��С
    float data_buffer[MOVE_AVERAGE_SIZE];  //������
    float data_sum;         //���ݺ�
    float data_average;     //����ƽ��ֵ
}move_filter_struct;

void move_filter_init(move_filter_struct *move_average);
void move_filter_calc(move_filter_struct *move_average, signed int new_data);


typedef struct
{
    unsigned char index;            //�±�
    unsigned char buffer_size;      //buffer��С
    double data_buffer[MOVE_AVERAGE_SIZE];  //������
    double data_sum;         //���ݺ�
    double data_average;     //����ƽ��ֵ
}move_filter_double_struct;

                /*******���������˲������********/
                /*******���������˲������********/
                /*******���������˲������********/
//typedef struct
//{
//    float LastP;//�ϴι���Э���� ��ʼ��ֵΪ0
//    float Now_P;//��ǰ����Э���� ��ʼ��ֵΪ0
//    float out;	//�������˲������ ��ʼ��ֵΪ0
//    float Kg;	//���������� ��ʼ��ֵΪ0
//    float Q;	//��������Э���� 
//    float R;	//�۲�����Э���� 
//}KFP;//Kalman Filter parameter
//float kalmanFilter(KFP *kfp,float input);
typedef struct EKF_filter
{
	float LastP;
	float Now_P;
	float out;
	float Kg;
	float Q;
	float R;	
}EKF_filter;
void Kalman(struct EKF_filter *ekf,float input);  //һά������


                /*******PID���********/
                /*******PID���********/
                /*******PID���********/

/*λ��ʽ*/
typedef struct
{
  float Kp;                       //����ϵ��Proportional
  float Ki;                       //����ϵ��Integral
  float Kd;                       //΢��ϵ��Derivative

  float Ek;                       //��ǰ���
  float Ek1;                      //ǰһ����� e(k-1)
  float Ek2;                      //��ǰһ����� e(k-2)
  float LocSum;                   //�ۼƻ���λ��
  float LIMT;                     //�����޷�
}PID_Loc_str;

/*����ʽ*/
typedef struct
{
  volatile float   Kp;             // �������� Proportional Const
  volatile float   Ki;               // ���ֳ��� Integral Const
  volatile float   Kd;             // ΢�ֳ��� Derivative Const
           float Pid_Out;
  volatile float      Error1;                 // Error[n-1]
  volatile float      Error2;                 // Error[n-2]
  volatile float      Error;                 // Error[n]
}PID_Inc_str;

float PID_Loc(float SetValue, float ActualValue, PID_Loc_str *PID);
float PID_Inc(float SetPoint,float CurrentPoint, PID_Inc_str* PID);

void move_filter_double_init(move_filter_double_struct *move_average);
void move_filter_double_calc(move_filter_double_struct *move_average, double new_data);


//��ͨ�˲���
float Low_Pass_Filter(float Data,Pass_Str* Filter);
//������ʼ��
void Parameter_Init(void);

#endif /* CODE_ALGORITHM_ALGORITHM_H_ */
